<?php
namespace Home\Controller;
use Home\Controller\IndexController;
class SaleChanceController extends IndexController 
{
    public function lst() {
        //传递页值
        $_p=I('get.p')|$_p=I('post.p');
        $p=$_p?$_p:'1';
        $this->assign('p',$p);
        //取出数据
        $model = D('SaleChance');
    	$data = $model->search();
        
        $this->assign(array(
        'data' => $data['data'],
        'page' => $data['page'],
    	));
        $this->display();
    }
    
    public function add()
    {
        //传递页值
        $_p=I('get.p')|$_p=I('post.p');
        $p=$_p?$_p:'1';
        $this->assign('p',$p);
        
    	if(IS_POST)
    	{
            $model = D('SaleChance');
            if($model->create(I('post.'), 1))
            {
                if($id = $model->add())
                {
                    $this->success('添加成功！', U('lst?p='.$p));
                    exit;
                }
            }
            $this->error($model->getError());
    	}
        $name = session('username');
        $time = time();//时间戳
        $nowtime = date('Y-m-d H:i:s',$time);//生成带格式的日期
       
        $this->assign('name',$name);
        $this->assign('nowtime',$nowtime);
        $this->display();
    
    }
    public function edit()
    {
        //传递页值
        $_p=I('get.p')|$_p=I('post.p');
        $p=$_p?$_p:'1';
        $this->assign('p',$p);
        
    	$id = I('get.id');
    	if(IS_POST)
    	{
            $model = D('SaleChance');
            if($model->create(I('post.'), 2))
            {
                if($model->save() !== FALSE)
                {
                    $this->success('修改成功！', U('lst', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
    	}
    	$model = M('SaleChance');
    	$data = $model->find($id);
    	$this->assign('data', $data);
        
        $this->display();
    }
    public function giveTo()
    {
        //传递页值
        $_p=I('get.p')|$_p=I('post.p');
        $p=$_p?$_p:'1';
        $this->assign('p',$p);
        
    	$id = I('get.id');
    	if(IS_POST)
    	{
            //修改为时间戳
            $model = D('SaleChance');
            $post = I('post.');
            $post['give_time'] = strtotime($post['give_time']);
            if($model->validate($model->rules)->create(I('post.'), 7))
            {
                if($model->save() !== FALSE)
                {
                    $this->success('修改成功！', U('lst', array('p' =>$p)));
                    exit;
                }
                
            }
            $this->error($model->getError());
    	}
    	$model = M('SaleChance');
    	$data = $model->find($id);
    	$this->assign('data', $data);
        
        //当前时间
        $name = session('username');
        $time = time();//时间戳
        $nowtime = date('Y-m-d H:i:s',$time);//生成带格式的日期
        $this->assign('nowtime',$nowtime);
        //查出客户经理
        $aModel=M('Admin');
        $adata = $aModel->field('a.id,a.username')->alias('a')->join('left join crm_admin_role b on a.id=b.admin_id')->where('role_id=4')->select();
        $this->assign('adata',$adata);
        
        $this->display();
    }
    
    public function delete()
    {
        //传递页值
        $_p=I('get.p')|$_p=I('post.p');
        $p=$_p?$_p:'1';
        $this->assign('p',$p);
    	$model = D('SaleChance');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
            $this->success('删除成功！', U('lst', array('p' => $p)));
            exit;
    	}
    	else 
    	{
            $this->error($model->getError());
    	}
    }   
}


